//
//  MomentsViewModel.m
//  AnimationRecommend
//
//  Created by 花花蔡 on 2017/2/22.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "MomentsViewModel.h"

@implementation MomentsViewModel

- (void)setMoment:(Moments *)moment {
    _moment = moment;
    
    [self setMomentsBodyFrame];
    [self setMomentsToolBarFrame];
    [self setCellHeight];
}

/**
 *  主体frame
 */
- (void)setMomentsBodyFrame {
 
    //头像
    CGFloat iconX_Y = bodyCellMargin;
    CGFloat iconW_H = bodyCellIconW_H;
    self.bodyIconFrame = CGRectMake(iconX_Y, iconX_Y, iconW_H, iconW_H);
    
    //昵称
    CGFloat nameX = bodyCellIconW_H + bodyCellMargin * 2;
    CGFloat nameY = iconX_Y;
    
    CGSize nameSize = [self.moment.userName sizeWithAttributes:circleCellNameattributes];
    self.bodyNameFrame = (CGRect){{nameX,nameY},nameSize};
    
    //时间
    CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(self.bodyNameFrame) + bodyCellMargin * 0.5;
    CGSize timeSize = [self.moment.createdAt boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:circleCellNameattributes context:nil].size;
    self.bodyTimeFrame = (CGRect){{timeX,timeY},timeSize};
    
    //正文
    CGFloat textX = bodyCellMargin;
    CGFloat textY = CGRectGetMaxY(self.bodyIconFrame) + bodyCellMargin;
    CGFloat textW = circleCellWidth - bodyCellMargin * 2;
    CGSize textSize = [self.moment.text boundingRectWithSize:CGSizeMake(textW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:circleCellTextattributes context:nil].size;
    self.bodyTextFrame = (CGRect){{textX,textY},textSize};
    
    //图片
    if ([[self.moment.photos objectAtIndex:0] count] != 0) {
        CGFloat photosX = bodyCellMargin * 2;
        CGFloat photoY = CGRectGetMaxY(self.bodyTextFrame) + bodyCellMargin;
        CGFloat photoW = textW;
        if ([[self.moment.photos objectAtIndex:0] count] > 6) {
            CGFloat photosH = circleCellPhotosWH * 3;
            self.bodyPhotoFrame = CGRectMake(photosX, photoY, photoW, photosH);
        } else if ([[self.moment.photos objectAtIndex:0] count] > 3) {
            CGFloat photosH = circleCellPhotosWH * 2;
            self.bodyPhotoFrame = CGRectMake(photosX, photoY, photoW, photosH);
        }else if ([[self.moment.photos objectAtIndex:0] count] > 2){
            CGFloat photosH = circleCellPhotosWH;
            self.bodyPhotoFrame = CGRectMake(photosX, photoY, photoW, photosH);
        }else if ([[self.moment.photos objectAtIndex:0] count] > 1) {
            CGFloat photosH = circleCellPhotosWH;
            self.bodyPhotoFrame = CGRectMake(photosX, photoY, photosH * 2, photosH);
        }else {
            CGFloat photosH = circleCellPhotosWH;
            self.bodyPhotoFrame = CGRectMake(photosX, photoY, photosH , photosH);
        }
        
        //主体frame
        CGFloat bodyH = CGRectGetMaxY(self.bodyPhotoFrame) + bodyCellMargin;
        self.momentsBodyFrame = CGRectMake(0, 0, circleCellWidth, bodyH);
    } else {
        //主体frame
        CGFloat bodyH = CGRectGetMaxY(self.bodyTextFrame) + bodyCellMargin;
        self.momentsBodyFrame = CGRectMake(0, 0, circleCellWidth, bodyH);
    }
}

/**
 *  工具条frame
 */
-(void)setMomentsToolBarFrame {
    //工具条frame
    CGFloat toolBarX = 0;
    CGFloat toolBarY = CGRectGetMaxY(self.momentsBodyFrame);
    CGFloat toolBarW = circleCellWidth;
    CGFloat toolBarH = circleCellToolBarHeight;
    self.momentsToolBarFrame = CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH);
    //点赞
    CGFloat likeX = 0;
    CGFloat likeY = 0;
    CGFloat likeW = toolBarW / 2;
    CGFloat likeH = circleCellToolBarHeight;
    self.toolLikeFrame = CGRectMake(likeX, likeY, likeW, likeH);
    //评论
    CGFloat commentX = likeW;
    CGFloat commentY = 0;
    CGFloat commentW = likeW;
    CGFloat commentH = likeH;
    self.toolCommentFrame = CGRectMake(commentX, commentY, commentW, commentH);

}

/**
 *  cell height
 */
- (void)setCellHeight {
    self.cellHeight = CGRectGetMaxY(self.momentsToolBarFrame);
}


@end
